require('dotenv').config();
const QianwenService = require('./src/services/qianwenService');

// 确保环境变量被正确加载
process.env.QIANWEN_API_KEY = process.env.QIANWEN_API_KEY || 'sk-361eb97a35ba4ef4bf2809c213e1956e';

async function testQianwenAPI() {
  console.log('🧪 开始测试通义千问API集成...\n');
  
  try {
    // 初始化服务
    const qianwenService = new QianwenService();
    console.log('✅ 通义千问服务初始化成功');
    
    // 测试连接
    console.log('🔗 测试API连接...');
    const isConnected = await qianwenService.testConnection();
    
    if (isConnected) {
      console.log('✅ API连接测试成功！');
      
      // 测试评分功能
      console.log('📝 测试评分功能...');
      const mockExercise = {
        title: '社交媒体对青少年的影响',
        content: 'Some people think that social media has a negative impact on teenagers. Discuss both views and give your opinion.',
        type: 'Task 2',
        difficulty: 'medium'
      };
      
      const mockEssay = `
Social media has become an integral part of teenagers' lives in recent years. While some argue that it has negative effects on young people, others believe it brings more benefits than harm.

On one hand, social media can have detrimental effects on teenagers. Firstly, it can lead to addiction and excessive screen time, which may affect their physical health and sleep patterns. Secondly, it exposes young people to cyberbullying and online harassment, which can cause psychological distress. Thirdly, social media often presents unrealistic standards of beauty and success, leading to low self-esteem and body image issues.

On the other hand, social media also provides numerous benefits for teenagers. It allows them to connect with friends and family, especially those who live far away. It also provides access to educational content and learning resources. Furthermore, social media can be a platform for self-expression and creativity.

In conclusion, while social media has some negative aspects, its benefits outweigh the drawbacks when used responsibly. Parents and educators should guide teenagers to use social media wisely and safely.
      `;
      
      const result = await qianwenService.scoreEssay(mockExercise, mockEssay);
      
      console.log('🎯 评分结果:');
      console.log(`   总分: ${result.overall}`);
      console.log(`   任务完成: ${result.taskAchievement}`);
      console.log(`   连贯性: ${result.coherence}`);
      console.log(`   词汇: ${result.vocabulary}`);
      console.log(`   语法: ${result.grammar}`);
      console.log(`   反馈: ${result.feedback}`);
      console.log(`   优点: ${result.strengths.join(', ')}`);
      console.log(`   改进建议: ${result.improvements.join(', ')}`);
      
      console.log('\n🎉 通义千问API集成测试完成！');
      
    } else {
      console.log('❌ API连接测试失败');
    }
    
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
    console.error('详细错误:', error);
  }
}

// 运行测试
testQianwenAPI();
